package com.realestate.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.realestate.common.RealEstateContants;
import com.realestate.common.ServiceObject;
import com.realestate.common.SpringContext;
import com.realestate.delegate.IBusinessDelegate;
import com.realestate.model.LoginModel;

@Controller
public class LoginController {

	private IBusinessDelegate delegate;

	public LoginController() {
		delegate = (IBusinessDelegate) SpringContext
				.getBean(RealEstateContants.LOGIN_DELEGATE);
	}

	@RequestMapping("/validateLogin")
	public ModelAndView processLogin(
			@ModelAttribute("loginModel") LoginModel loginModel) {

		ServiceObject srvObj = new ServiceObject();
		srvObj.setActionMode(RealEstateContants.LOGIN);
		srvObj.setDataObject(loginModel);
		delegate.performTask(srvObj);
		String retView = ((boolean) srvObj.getDataObject()) ? "success"
				: "fail";
		return new ModelAndView(retView);

	}

	@RequestMapping("/index")
	public ModelAndView loginHome(){

		return new ModelAndView();

	}

	/**
	 * @return the delegate
	 */
	public IBusinessDelegate getDelegate() {
		return delegate;
	}

	/**
	 * @param delegate
	 *            the delegate to set
	 */
	public void setDelegate(IBusinessDelegate delegate) {
		this.delegate = delegate;
	}

}
